﻿using MIMSDomain.Abstract;
using MIMSDomain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MIMSCore.Abstract;
using MIMSCore.Logging;

namespace MIMSWeb.Controllers
{
    [Authorize]
    public class MedicalPersonnelsController : Controller
    {
        IRepository<MedicalPersonnel> _personnelRepository;
        ILogger<SynkronLogger> _logger;

        public MedicalPersonnelsController(IRepository<MedicalPersonnel> personnelRepository, ILogger<SynkronLogger> logger)
        {
            _personnelRepository = personnelRepository;
            _logger = logger;
        }

        public ActionResult Index()
        {
            var model = _personnelRepository.All;
            return View("Index",model);
        }

        public ViewResult AddMedicalPersonnel()
        {
            return View();
        }

        [HttpPost]
        //TO DO: update Model to view model...
        public ViewResult AddMedicalPersonnel(MedicalPersonnel Model)
        {
            if (ModelState.IsValid)
            {
                try { 
                    //add medical personnel
                    _personnelRepository.Save(Model);
                    _logger.WriteInformation(2000, "Medical Personnel Created Successfully: ", 
                        new Dictionary<string, object> {{"Personnel Details", Model}});
                }
                catch(Exception Ex){
                    //Log Exception...
                    _logger.WriteCritical(4000, "Error Adding Medical Personnel",Ex);
                    ModelState.AddModelError("", Ex.Message);  
                }
            }
            else
            {
                ModelState.AddModelError("", "ModelState Invalid");
                _logger.WriteWarning(6000, "Error Adding Medical Personnel");
            }

            return View(Model);
        }

    }
}
